import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  build: {
    outDir: 'dist',
    emptyOutDir: true,
    sourcemap: true,
    // 允许CSS代码分割，确保CSS文件能被正确处理
    cssCodeSplit: true,
    css: {
      // 允许提取CSS到单独文件
      extract: true
    },
    lib: {
      entry: resolve(__dirname, 'src/index.ts'),
      name: 'Morghulis',
      fileName: (format) => `morghulis.${format}.js`
    },
    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      external: [
          'vue',
          'element-plus',
          'lodash',
          '@fortawesome/fontawesome-svg-core',
          '@fortawesome/free-solid-svg-icons',
          '@fortawesome/vue-fontawesome',
          'js-cookie',
          'axios',
          'nprogress',
          'nanoid',
          'font-awesome'
        ],
      output: {
        // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
        globals: {
          vue: 'Vue',
          'element-plus': 'ElementPlus',
          'lodash': '_',
          '@fortawesome/fontawesome-svg-core': 'FontawesomeCore',
          '@fortawesome/free-solid-svg-icons': 'FontawesomeIcons',
          '@fortawesome/vue-fontawesome': 'FontawesomeVue',
          'js-cookie': 'Cookies',
          'axios': 'axios',
          'nprogress': 'NProgress',
          'nanoid': 'nanoid',
          'font-awesome': 'FontAwesome'
        },
        // 确保CSS文件被正确输出
        assetFileNames: (assetInfo) => {
          if (assetInfo.name === 'index.css') return 'style.css';
          return assetInfo.name;
        }
      }
    }
  }
})